package ru.lukyanets.torrent.bencode;


import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author slukyanets
 */
public class BencodeTest {

    @Test
    public void testDecode() throws IOException {
        InputStream is = getClass().getClassLoader().getResourceAsStream("test.torrent");
        Assertions.assertNotNull(is);
        Object obj = BDecoder.decode(is);

        Assertions.assertNotNull(obj);
        Assertions.assertTrue(obj instanceof Map);
        Assertions.assertNotNull(((Map<?, ?>) obj).get("announce"));
    }

    @Test
    public void testEncode() throws IOException {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("a", 42);
        List<Object> list = new LinkedList<>();
        list.add("A");
        list.add(12);
        list.add("B");
        map.put("b", list);

        Map<String, Object> map2 = new LinkedHashMap<>();
        map2.put("c", "C");
        map2.put("d", 11);
        map.put("cc", map2);

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        BEncoder.encode(map, os);
        Assertions.assertTrue(os.size() > 0);
        Assertions.assertEquals("d1:ai42e1:bl1:Ai12e1:Be2:ccd1:c1:C1:di11eee", os.toString());
    }
}
